﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class ProfessionArea :BasePage
    {
        readonly Card theCard = new Card();
        readonly OrderAccount theOrderAccount = new OrderAccount();

        public StringBuilder sProductHtml = new StringBuilder();
        readonly DALShoppro theShoppro = new DALShoppro();
        private static readonly Regex RX = new Regex("&amp;page=d+", RegexOptions.Compiled);

        public string sLoginMessage = "温馨提示：请输入礼品卡卡号和密码，选取您要兑换的商品！(可同时输入多张卡)</br></br>";
        string _builderOutPageHeaderHtml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["backHref"] = Request.Url.PathAndQuery;
         
          
            if (!IsPostBack)
            {

                if (Request.QueryString["type"] == "k")
                {
                    this.div_show.InnerHtml = "<script langueage='javascript'>TB_show(null, \"/cn/exchange.aspx?TB_iframe=true&height=370&width=640\", false);</script>";
                }
                if (!string.IsNullOrEmpty(Request.QueryString["pid"]) && string.IsNullOrEmpty(Request.QueryString["isView"]))
                {
                    this.category_1.Visible = false;
                 //   this.productsList.Visible = false;
                    return;
                }
                _builderOutPageHeaderHtml = GetBuilderOutPageHeaderHtml();
                this.AspNetPager1.CurrentPageIndex = 1;
                this.AspNetPager2.CurrentPageIndex = 1;
               
             //   BindDataList(1);
            }
        }


        public string ArrowDirectionSale
        {
            get
            {
                if (this.Context.Session["ArrowDirectionSale"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionSale"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionSale"] = value;
            }
        }

        public string ArrowDirectionPrice
        {
            get
            {
                if (this.Context.Session["ArrowDirectionPrice"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionPrice"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionPrice"] = value;
            }
        }

         string GetBuilderOutPageHeaderHtml()
        {

                string input = this.Context.Request.Url.Query.Replace("?", "&amp;");
                input = RX.Replace(input, string.Empty);
                input = input.Replace("&amp;order=sale", string.Empty);
                input = input.Replace("&amp;order=price", string.Empty);
                input = input.Replace("&amp;order=time", string.Empty);
                string sLiClass1 = string.Empty;
                string sLiClass2 = string.Empty;
                string sLiClass3 = string.Empty;
                string sArrow1 = string.Empty;
                string sArrow2 = string.Empty;
                if (string.IsNullOrEmpty(this.Context.Request["order"]) || this.Context.Request["order"] == "sale")
                {
                    if (!string.IsNullOrEmpty(this.Context.Request["order"]))
                    {
                        if (ArrowDirectionSale == "&darr;")
                        {
                            ArrowDirectionSale = "&uarr;";
                        }
                        else
                        {
                            ArrowDirectionSale = "&darr;";
                        }
                    }
                    sArrow1 = ArrowDirectionSale;
                    sLiClass1 = "class='currentPage'";
                    sArrow2 = string.Empty;
                }
                else if (this.Context.Request["order"] == "price")
                {
                    if (ArrowDirectionPrice == "&darr;")
                    {
                        ArrowDirectionPrice = "&uarr;";
                    }
                    else
                    {
                        ArrowDirectionPrice = "&darr;";
                    }
                    sArrow2 = ArrowDirectionPrice;
                    sArrow1 = string.Empty;
                    sLiClass2 = "class='currentPage'";
                }
                else if (this.Context.Request["order"] == "time")
                {
                    sLiClass3 = "class='currentPage'";
                }
                StringBuilder sbPageHeader = new StringBuilder();
                sbPageHeader.Append("  <div style=\" width:100%; text-align:left;  height:19px; padding-left:5px; float:left\">");
                sbPageHeader.Append("  <div style='float:left;'>排序</div>");
                sbPageHeader.Append("   <div id=\"orderMenu\">");
                sbPageHeader.Append("  <ul>");
                sbPageHeader.Append(string.Format("  <li {1}><a href=?order=sale{0}>{4}销  量</a></li><li {2}><a href=?order=price{0}>{5}价  格</a></li><li {3}><a href=?order=time{0}>上价时间</a></li>", input, sLiClass1, sLiClass2, sLiClass3, sArrow1, sArrow2));
                sbPageHeader.Append("  </ul>");
                sbPageHeader.Append("  </div>");

                sbPageHeader.Append(" </div>");

                return sbPageHeader.ToString();
                //System.Web.UI.HtmlControls.HtmlGenericControl divHtml = new System.Web.UI.HtmlControls.HtmlGenericControl();
                //divHtml.InnerHtml = sbPageHeader.ToString();
            
        }

    

        void BindDataList(int iPageType)
        {

            int iCardtypeId = -1;
            StringBuilder sCardPrices = new StringBuilder();
      

            if (base.LoginUserMember != null && base.LoginUserMember.DicUserCardList != null)
            {
                int i = 0;
                foreach (dbCard o in base.LoginUserMember.DicUserCardList.Values)
                {
                    if (i == 0)
                    {
                        iCardtypeId = o.cardtypeId;
                        sCardPrices.Append(o.cardprice.ToString());
                    }
                    else
                    {
                        sCardPrices.AppendFormat(",{0}", o.cardprice.ToString());
                    }
                    i++;
                }
            }


            DataSet ds = theShoppro.GetDataListPage(base.Request.QueryString["sid"]
                                                 , Request.QueryString["typeid"]
                                                 , Request.QueryString["order"]
                                                 , ArrowDirectionSale
                                                 , ArrowDirectionPrice
                                                 , this.AspNetPager1.StartRecordIndex - 1
                                                 , this.AspNetPager1.StartRecordIndex * this.AspNetPager1.PageSize
                                                 , iCardtypeId
                                                 , sCardPrices.ToString()
                                                 , Request.QueryString["key"]
                                                 );


            if (ds != null)
            {
                if (iPageType == 1)
                {
                    AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
                    AspNetPager2.RecordCount = AspNetPager1.RecordCount;
                }
                else if (iPageType == 2)
                {
                    AspNetPager2.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
                    AspNetPager1.RecordCount = AspNetPager2.RecordCount;
                }
                BindProfessionProductList(ds.Tables[0]);
             
            }


            this.tdOrder.InnerHtml = _builderOutPageHeaderHtml;

        }

        void BindProfessionProductList(DataTable dtProduct)
        {
            sProductHtml = new StringBuilder();
            if (dtProduct == null || dtProduct.Rows.Count <= 0) return;

                sProductHtml.Append("<div class=\"info_list\"><ul>");
                string sProName = string.Empty;
                foreach (DataRow dr in dtProduct.Rows)
                {
                    sProName = dr["proName"].ToString().Length > 12 ? dr["proName"].ToString().Substring(0, 12) + ".." : dr["proName"].ToString();
                    sProductHtml.AppendFormat("<li>"
                                    + " <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"130\" height=\"130\" alt=\"\" src=\"{2}\" /></a>"
                                    + " <p><a href=\"{0}\" target=\"_blank\" title=\"{7}\">{3}</a><em>库存：{4}</em><em>兑换价：￥{5}</em></p>"
                                    + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品编号:{6}</span> <span><em>库存：{4}</em></span><span><em>兑换价￥{5}</em></span></p></div>"
                                    + "</li>"
                                    , "products_details.aspx?pid=" + dr["proid"].ToString() + "&sid=" + Request.QueryString["sid"]
                                    , dr["proid"].ToString()
                                    , "/uploadfile/product/" + dr["propic2"].ToString()
                                    , sProName
                                    , dr["proku1"].ToString()
                                    , String.Format("{0:f}", dr["proprice1"].ToString())
                                    , dr["procode"].ToString()
                                    , dr["proName"].ToString()
                                    );
                }

                sProductHtml.Append("</ul></div>");//<div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
            }



        
        


        //void BuildProductList()
        //{
        //    DataSet ds;
          
        //        ds = theShoppro.GetListForProfession(Request.QueryString["typeid"]);
        //    if (ds.Tables.Count > 0)
        //    {
        //        this.productsList.DataSource = ds.Tables[0].DefaultView;
        //        this.productsList.DataBind();
        //    }
        //}


        //protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        //{
        //    dbCard oCard;
        //    this.productsList.CurrentPageIndex = e.NewPageIndex;
            
        //    if (UserCardCodeList != null)
        //    {
        //        decimal dTotalCardPrice = 0;
        //        for (int i = 0; i < UserCardCodeList.Count; i++)
        //        {
        //            dTotalCardPrice += decimal.Parse(UserCardCodeList[i].UserCardPrice);
        //        }
        //        this.lblMoneyMessage.Text = "您的礼品卡余额为:" + dTotalCardPrice.ToString() + ",请选取您要购买的商品！";
        //        oCard = theCard.GetModel(UserCardCodeList[0].UserCardCode, UserCardCodeList[0].UserCardPassword);
              
        //        this.BuildProductList(oCard.cardtypeId);
        //    }
        //    else
        //    {
        //        this.BuildProductList();
        //    }

        //}



        public void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Label label = (Label)e.Item.FindControl("price");
                Label label2 = (Label)e.Item.FindControl("Label1");
                Image image = (Image)e.Item.FindControl("Image2");
                if ((label.Text == "0") || (label.Text == "0.00"))
                {
                    label.Text = label2.Text;
                }
                if (image.ImageUrl.ToString() == "")
                {
                    image.ImageUrl = "../uploadfile/banner/" + info.info1("", "shopmopic");
                }

            }
        }


        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session.Clear();   
            Response.Redirect("index.aspx");
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            this.AspNetPager2.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex;
            this.BindDataList(1);

        }

        protected void AspNetPager2_PageChanged(object src, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = this.AspNetPager2.CurrentPageIndex;
            this.BindDataList(2);

        }

    }
}